package com.yjc.hire.servicefile.controller;

import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.servicefile.BO.Base64FileBO;
import com.yjc.hire.servicefile.config.MinioConfig;
import com.yjc.hire.servicefile.utils.MinIoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/21
 * </p>
 */

@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private MinioConfig minioConfig;

    public static final String host = "http://127.0.0.1:8080";

    @PostMapping("uploadFace1")
    public GraceJSONResult uploadFaceDemo(@RequestParam("file") MultipartFile file,
                                      @RequestParam("userId") String userId,
                                      HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        String newFileName = userId + suffixName;
        String rootPath = "/temp" + File.separator;
        String filePath = rootPath + File.separator + "face" + File.separator + newFileName;
        File newFile = new File(filePath);
        if (!newFile.getParentFile().exists()){
            newFile.getParentFile().mkdir();
        }
        file.transferTo(newFile);

        String userFaceUrl = host + "static/face/" + newFileName;
        return GraceJSONResult.ok(userFaceUrl);
    }
    @PostMapping("uploadFace")
    public GraceJSONResult uploadFace(@RequestParam("file") MultipartFile file,
                                      @RequestParam("userId") String userId) throws Exception {
        if (StringUtils.isBlank(userId)){
            return GraceJSONResult.exception(ResponseStatusEnum.FILE_UPLOAD_FAILD);
        }
        String filename = file.getOriginalFilename();
        if (StringUtils.isBlank(filename)){
            return GraceJSONResult.exception(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        filename = userId + File.separator + filename;

        MinIoUtils.uploadFile(minioConfig.getBucketName(),filename,file.getInputStream());
        String imageUrl = minioConfig.getFileHost() + "/"
        + minioConfig.getBucketName()
        + "/" + filename;
        return GraceJSONResult.ok(imageUrl);
    }

    @PostMapping("uploadAdminFace")
    public GraceJSONResult uploadAdminFace(@RequestBody Base64FileBO base64FileBO) throws Exception {
        String base64 = base64FileBO.getBase64File();
        String suffixName = ".png";
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid + suffixName;
        String rootPath = "/temp" + File.separator;
        String filePath = rootPath + File.separator
                + "adminFace" + File.separator
                + fileName;
        String imageUrl = minioConfig.getFileHost() + "/"
                + minioConfig.getBucketName()
                + "/" + fileName;
        return GraceJSONResult.ok(imageUrl);
    }
    @PostMapping("uploadLogo")
    public GraceJSONResult uploadLoge(@RequestParam("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (StringUtils.isBlank(filename)){
            return GraceJSONResult.exception(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        MinIoUtils.uploadFile(minioConfig.getBucketName(),filename,file.getInputStream());
        String imageUrl = minioConfig.getFileHost() + "/"
                + minioConfig.getBucketName()
                + "/" + filename;
        return GraceJSONResult.ok(imageUrl);
    }
    @PostMapping("uploadBizLicense")
    public GraceJSONResult uploadBizLicense(@RequestParam("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        if (StringUtils.isBlank(filename)){
            return GraceJSONResult.exception(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
        }
        String imageUrl = MinIoUtils.uploadFile(minioConfig.getBucketName(), filename, file.getInputStream(), true);
        return GraceJSONResult.ok(imageUrl);
    }

    @PostMapping("uploadPhoto")
    public GraceJSONResult uploadPhoto(@RequestParam("file") MultipartFile[] file,
                                       String companyId) throws Exception {
        List<String> fileList = new ArrayList<>();
        if (StringUtils.isBlank(companyId)) companyId = "";
        for (MultipartFile multipartFile : file) {
            String filename = multipartFile.getOriginalFilename();
            filename = "company/" + companyId + "/photo" + dealFileName(filename);
            String imageUrl = MinIoUtils.uploadFile(minioConfig.getBucketName(),
                    filename,multipartFile.getInputStream(),true);
            fileList.add(imageUrl);
        }
        return GraceJSONResult.ok(fileList);
    }

    private String dealFileName(String fileName) {
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String fName = fileName.substring(0, fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        return fName + "-" + uuid + suffixName;
    }
}
